<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
<meta name="description" content="Interactively adding, resizing, and removing rows and columns of a Table Panel in a GoJS Node."/> 
<link rel="stylesheet" href="../assets/css/style.css"/> 
<!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
<title>Add or Remove Columns</title>
</head>

<body>
  <!-- This top nav is not part of the sample code -->
  <nav id="navTop" class="w-full z-30 top-0 text-white bg-nwoods-primary">
    <div class="w-full container max-w-screen-lg mx-auto flex flex-wrap sm:flex-nowrap items-center justify-between mt-0 py-2">
      <div class="md:pl-4">
        <a class="text-white hover:text-white no-underline hover:no-underline
        font-bold text-2xl lg:text-4xl rounded-lg hover:bg-nwoods-secondary " href="../">
          <h1 class="mb-0 p-1 ">GoJS</h1>
        </a>
      </div>
      <button id="topnavButton" class="rounded-lg sm:hidden focus:outline-none focus:ring" aria-label="Navigation">
        <svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6">
          <path id="topnavOpen" fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM9 15a1 1 0 011-1h6a1 1 0 110 2h-6a1 1 0 01-1-1z" clip-rule="evenodd"></path>
          <path id="topnavClosed" class="hidden" fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
        </svg>
      </button>
      <div id="topnavList" class="hidden lg:text-base sm:block items-center w-auto mt-0 text-white p-0 z-20">
        <ul class="list-reset list-none font-semibold flex justify-end flex-wrap sm:flex-nowrap items-center px-0 pb-0">
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../learn/">Learn</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../samples/">Samples</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../intro/">Intro</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../api/">API</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/products/register.html">Register</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../download.html">Download</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://forum.nwoods.com/c/gojs/11">Forum</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/contact.html"
           target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/sales/index.html"
           target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a></li>
        </ul>
      </div>
    </div>
    <hr class="border-b border-gray-600 opacity-50 my-0 py-0" />
  </nav>
  <div class="md:flex flex-col md:flex-row md:min-h-screen w-full max-w-screen-xl mx-auto">
    <div id="navSide" class="flex flex-col w-full md:w-48 text-gray-700 bg-white flex-shrink-0"></div>
    <!-- * * * * * * * * * * * * * -->
    <!-- Start of GoJS sample code -->
    
    <script src="../release/go.js"></script>
    <div class="p-4 w-full">
<script id="code">
  function init() {
    if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this
    var $ = go.GraphObject.make;

    myDiagram =
      $(go.Diagram, "myDiagramDiv",
        { "undoManager.isEnabled": true });

    myDiagram.nodeTemplate =
      $(go.Node, "Auto",
        $(go.Shape, { fill: "white" }),
        $(go.Panel, "Table",
          new go.Binding("itemArray", "people"),
          $(go.RowColumnDefinition,
            { row: 0, background: "lightgray" }),
          $(go.RowColumnDefinition,
            { row: 1, separatorStroke: "black" }),
          // the table headers -- remains even if itemArray is empty
          $(go.Panel, "TableRow",
            { isPanelMain: true },
            new go.Binding("itemArray", "columnDefinitions"),
            {
              itemTemplate:  // bound to a column definition object
                $(go.Panel,
                  new go.Binding("column"),
                  $(go.TextBlock,
                    { margin: new go.Margin(2, 2, 0, 2), font: "bold 10pt sans-serif" },
                    new go.Binding("text"))
                )
            }
          ),
          { // the rows for the people
            name: "TABLE",
            defaultAlignment: go.Spot.Left,
            defaultColumnSeparatorStroke: "black",
            itemTemplate:  // bound to a person/row data object
              $(go.Panel, "TableRow",
                // which in turn consists of a collection of cell objects,
                // held by the "columns" property in an Array
                new go.Binding("itemArray", "columns"),
                // you could also have other Bindings here for the whole row
                {
                  itemTemplate:  // bound to a cell object
                    $(go.Panel,  // each of which as "attr" and "text" properties
                      { stretch: go.GraphObject.Fill, alignment: go.Spot.TopLeft },
                      new go.Binding("column", "attr",
                        function(a, elt) {  // ELT is this bound item/cell Panel
                          // elt.data will be the cell object
                          // elt.panel.data will be the person/row data object
                          // elt.part.data will be the node data object
                          // "columnDefinitions" is on the node data object, so:
                          var cd = findColumnDefinitionForName(elt.part.data, a);
                          if (cd !== null) return cd.column;
                          throw new Error("unknown column name: " + a);
                        }),
                      // you could also have other Bindings here for this cell
                      $(go.TextBlock, { editable: true },
                        { margin: new go.Margin(2, 2, 0, 2), wrap: go.TextBlock.None },
                        new go.Binding("text").makeTwoWay())
                    )
                }
              )
          }
        )
      );

    myDiagram.model =
      $(go.GraphLinksModel,
        {
          copiesArrays: true,
          copiesArrayObjects: true,
          nodeDataArray: [
            { // first node
              key: 1,
              columnDefinitions: [
                // each column definition needs to specify the column used
                { attr: "name", text: "Name", column: 0 },
                { attr: "phone", text: "Phone #", column: 1 },
                { attr: "office", text: "Office", column: 2 }
              ],
              people: [  // the table of people
                // each row is a person with an Array of Objects associating a column name with a text value
                { columns: [{ attr: "name", text: "Alice" }, { attr: "phone", text: "2345" }, { attr: "office", text: "C4-E18" }] },
                { columns: [{ attr: "name", text: "Bob" }, { attr: "phone", text: "9876" }, { attr: "office", text: "E1-B34" }] },
                { columns: [{ attr: "name", text: "Carol" }, { attr: "phone", text: "1111" }, { attr: "office", text: "C4-E23" }] },
                { columns: [{ attr: "name", text: "Ted" }, { attr: "phone", text: "2222" }, { attr: "office", text: "C4-E197" }] }
              ]
            },
            { // second node
              key: 2,
              columnDefinitions: [
                { attr: "name", text: "Name", column: 0 },
                { attr: "phone", text: "Phone #", column: 2 },  // note the different order of columns
                { attr: "office", text: "Office", column: 1 }
              ],
              people: [
                { columns: [{ attr: "name", text: "Robert" }, { attr: "phone", text: "5656" }, { attr: "office", text: "B1-A27" }] },
                { columns: [{ attr: "name", text: "Natalie" }, { attr: "phone", text: "5698" }, { attr: "office", text: "B1-B6" }] }
              ]
            }
          ],
          linkDataArray: [
            { from: 1, to: 2 }
          ]
        }
      );
  }

  // Add or remove a person row from the selected node's table of people.

  function insertIntoArray() {
    var n = myDiagram.selection.first();
    if (n === null) return;
    var d = n.data;
    myDiagram.startTransaction("insertIntoTable");
    // add item as second in the list, at index #1
    // of course this new data could be more realistic:
    myDiagram.model.insertArrayItem(d.people, 1, {
      columns: [{ attr: "name", text: "Elena" },
      { attr: "phone", text: "456" },
      { attr: "office", text: "LA" }]
    });
    myDiagram.commitTransaction("insertIntoTable");
  }

  function removeFromArray() {
    var n = myDiagram.selection.first();
    if (n === null) return;
    var d = n.data;
    myDiagram.startTransaction("removeFromTable");
    // remove second item of list, at index #1
    myDiagram.model.removeArrayItem(d.people, 1);
    myDiagram.commitTransaction("removeFromTable");
  }

  // add or remove a column from the selected node's table of people

  function findColumnDefinitionForName(nodedata, attrname) {
    var columns = nodedata.columnDefinitions;
    for (var i = 0; i < columns.length; i++) {
      if (columns[i].attr === attrname) return columns[i];
    }
    return null;
  }

  function findColumnDefinitionForColumn(nodedata, idx) {
    var columns = nodedata.columnDefinitions;
    for (var i = 0; i < columns.length; i++) {
      if (columns[i].column === idx) return columns[i];
    }
    return null;
  }

  function addColumn(attrname) {
    var n = myDiagram.selection.first();
    if (n === null) return;
    var d = n.data;
    // if name is not given, find an unused column name
    if (attrname === undefined || attrname === "") {
      attrname = "new";
      var count = 1;
      while (findColumnDefinitionForName(d, attrname) !== null) {
        attrname = "new" + (count++).toString();
      }
    }
    // find an unused column #
    var col = 3;
    while (findColumnDefinitionForColumn(d, col) !== null) {
      col++;
    }
    myDiagram.startTransaction("addColumn");
    var model = myDiagram.model;
    // add a column definition for the node's whole table
    model.addArrayItem(d.columnDefinitions, {
      attr: attrname,
      text: attrname,
      column: col
    });
    // add cell to each person in the node's table of people
    var people = d.people;
    for (var j = 0; j < people.length; j++) {
      var person = people[j];
      model.addArrayItem(person.columns, {
        attr: attrname,
        text: Math.floor(Math.random() * 1000).toString()
      });
    }
    myDiagram.commitTransaction("addColumn");
  }

  function removeColumn() {
    var n = myDiagram.selection.first();
    if (n === null) return;
    var d = n.data;
    var coldef = d.columnDefinitions[3];  // get the fourth column
    if (coldef === undefined) return;
    var attrname = coldef.attr;
    myDiagram.startTransaction("removeColumn");
    var model = myDiagram.model;
    model.removeArrayItem(d.columnDefinitions, 3);
    n.findObject("TABLE").removeColumnDefinition(coldef.column);
    // update columns for each person in this table
    var people = d.people;
    for (var j = 0; j < people.length; j++) {
      var person = people[j];
      var columns = person.columns;
      for (var k = 0; k < columns.length; k++) {
        var cell = columns[k];
        if (cell.attr === attrname) {
          // get rid of this attribute cell from the person.columns Array
          model.removeArrayItem(columns, k);
          break;
        }
      }
    }
    myDiagram.commitTransaction("removeColumn");
  }

  function swapTwoColumns() {
    myDiagram.startTransaction("swapColumns");
    var model = myDiagram.model;
    myDiagram.selection.each(function(n) {
      if (!(n instanceof go.Node)) return;
      var d = n.data;
      var phonedef = findColumnDefinitionForName(d, "phone");
      if (phonedef === null) return;
      var phonecolumn = phonedef.column;  // remember the column number
      var officedef = findColumnDefinitionForName(d, "office");
      if (officedef === null) return;
      var officecolumn = officedef.column;  // and this one too
      model.setDataProperty(phonedef, "column", officecolumn);
      model.setDataProperty(officedef, "column", phonecolumn);
      model.updateTargetBindings(d);  // update all bindings, to get the cells right
    });
    myDiagram.commitTransaction("swapColumns");
  }
  window.addEventListener('DOMContentLoaded', init);
</script>

<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:400px"></div>
<p>Add a row or Remove the second row of the table held by the selected node:</p>
<button onclick="insertIntoArray()">Insert Into Array</button>
<button onclick="removeFromArray()">Remove From Array</button>
<p>Add a column or Remove the fourth column from the table of the selected node:</p>
<button onclick="addColumn()">Add Column</button>
<button onclick="removeColumn()">Remove Column</button>
<p>Swap the "phone" and "office" columns for each selected node:</p>
<button onclick="swapTwoColumns()">Swap Two Columns</button>
<p>See also the <a href="../extensions/ColumnResizing.html">Column and Row Resizing Tools</a></p>
</div>
    </div>
    <!-- * * * * * * * * * * * * * -->
    <!--  End of GoJS sample code  -->
  </div>
</body>
<!--  This script is part of the gojs.net website, and is not needed to run the sample -->
<script src="../assets/js/goSamples.js"></script>
</html>
